/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BAWebBrowserViewController.m
//  redia-bapps-ios
//
//  Created by Mads Darø Kristensen on 10/22/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BAWebBrowserViewController.h"
#import "BALibrarySettings.h"
#import "BACustomNavigationBar.h"

@interface BAWebBrowserViewController ()

@end

@implementation BAWebBrowserViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if ([[BAUtils instance] iOSMajorVersion] < 7) {
        self.staturbarTintHeightConstraint.constant = 0;
    }
    
    self.topBarView.backgroundColor = [[BALibrarySettings instance] getCustomerColorNormal];
    self.mainBarView.backgroundColor = BA_CUSTOM_NAVBAR_DEFAULT_GRAY_COLOR;
    
    if (self.url) {
        NSURL *url = [NSURL URLWithString:self.url];
        if (url) {
            [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setUrl:(NSString *)url
{
    _url = url;
    if (self.isViewLoaded) {
        NSURL *url = [NSURL URLWithString:self.url];
        if (url) {
            [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
        }        
    }
}


#pragma mark - Button callbacks

- (IBAction)closeButtonPressed:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (IBAction)backButtonPressed:(id)sender
{
    [self.webView goBack];
}

- (IBAction)forwardButtonPressed:(id)sender
{
    [self.webView goForward];
}

#pragma mark - Webview delegate

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [self.activityIndicator startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.activityIndicator stopAnimating];
    
    // Update button state.
    self.forwardButton.enabled = [self.webView canGoForward];
    self.backButton.enabled = [self.webView canGoBack];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [self.activityIndicator stopAnimating];
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Fejl ved indlæsning" message:@"Der opstod en fejl ved indlæsningen af websiden. Prøv igen senere." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
//    [alert show];
}

@end
